<?php

namespace Dyadmin\Controller;

use Think\Controller;

class SystemController extends CommomController
{
    public function logs()
    {
        $this->check('1_3');

        $map['admin_name'] = I("get.admin_name");
        $map['timestar'] = I("get.timestar");
        $map['timeend'] = I("get.timeend");
        $map['log_ip'] = I("get.log_ip");

        if (IS_POST) {
            $map['admin_name'] = I("post.admin_name");
            $map['timestar'] = I("post.timestar");
            $map['timeend'] = I("post.timeend");
            $map['log_ip'] = I("post.log_ip");
            $_GET['p'] = 1;

        }
        //dump($_REQUEST);
        //die();
        if (!empty($map['timestar']) && !empty($map['timeend'])) {
            $stime1 = strtotime($map['timestar'] . ' 00:00:00');
            $etime1 = strtotime($map['timeend'] . ' 00:00:00');
            $where['add_time'] = array('between', array($stime1, $etime1));
        }
        if (!empty($map['admin_name'])) {
            $where['admin_name'] = $map['admin_name'];
        }
        if (!empty($map['log_ip'])) {
            $where['log_ip'] = ip2long($map['log_ip']);
        }
        $count = M("LogAdmin")->where($where)->count();
        $page = new \Think\Page ($count, 8, $map);
        $show = $page->show();
        $list = M("LogAdmin")->where($where)->order('log_id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign($map);
        $this->assign("page", $show);
        $this->assign("logs", $list);
        $this->display();
    }

    public function change_pwd()
    {

        $this->check('1_2');

        $this->display();

    }

    //執行密码修改操作
    public function update()
    {
        //登陆用户的密码
        $login_session = session('login_session');
        //用用户登陆的密码去查询用户的信息
        $admin_data = D("Admin")->isLogin($login_session);
        //登陆用户id
        $aid = $admin_data['admin_id'];
        if (IS_POST) {
            //获取表单提交过来原密碼
            $apwd = I('oldpwd');
            //获取表单提交过来的新密码
            $pwd = I('newpwd');
        }
        //給輸入的原密碼加密
        $apwds = sha1($apwd);
        //把新密码加密
        $datapwd = sha1($pwd);
        //查詢出數據庫裏面的密碼
        $user = M('admin')->where("admin_id=$aid")->getField('admin_pwd');
        //判斷輸入的原密碼跟數據庫裏面的密碼是否相同，如果不同，修改失敗！如果一樣，允許修改！
        if ($apwds == $user) {
            $data = [
                "admin_pwd" => $datapwd//新的密码
            ];
            if (M('admin')->data($data)->where("admin_id=$aid")->save()) {
                $this->success('修改成功！');
            } else {
                $this->error('修改失败！');
            }
        } else {
            $this->error('修改失败！原密碼錯誤！');
        }
    }


    public function admin()
    {
        $this->check('1_1');

        if (IS_POST) {
            $map['admin_name'] = I("post.admin_name");
            $map['timestar'] = I("post.timestar");
            $map['timeend'] = I("post.timeend");
            $_GET['p'] = 1;
        }
        if (!empty($map['timestar']) && !empty($map['timeend'])) {
            $stime1 = strtotime($map['timestar'] . ' 00:00:00');
            $etime1 = strtotime($map['timeend'] . ' 00:00:00');
            $where['reg_time'] = array('between', array($stime1, $etime1));
        }
        if (!empty($map['admin_name'])) {
            $where['admin_name'] = $map['admin_name'];
        }
        $count = M("Admin")->where($where)->count();
        $page = new \Think\Page ($count, 6, $map);
        $show = $page->show();

        $data = M('Admin')->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
        $role = M('AdminRole')->field("role_id,role_name")->select();

        foreach ($role as $val) {
            $r[$val['role_id']] = $val['role_name'];

        }
        $this->assign("page", $show);
        $this->assign("data", $data);
        $this->assign("role", $r);

        $this->display();

    }

//执行管理员的添加
    public function admin_add()
    {
        //实例化
        $admin = D('Admin');
        //先查询出权限表的role_id和role_name
        $role = M('AdminRole')->field("role_id,role_name")->select();
        //把查询出来的渲染到视图
        $this->assign("role", $role);
        if (IS_POST) {
            $data['role_id'] = I('post.role_id');
            $data['name'] = I('post.name');
            $data['admin_name'] = I('post.admin_name');
            $data['admin_pwd'] = sha1(I('post.admin_pwd'));
            $data['reg_time'] = date("Y-m-d H:i:s");
            $data['login_time'] = time();
            $data['login_num'] = 0;
            $data['login_session'] = NULL;
            $data['login_ip'] = get_client_ip();
            $data['status'] = 0;

            $username = $admin->where(array("admin_name" =>  $data['admin_name'] ))->select();
            //判断用户名是否存在
            if (!$username) {

                //判断role_id是否为0；如果为0，添加失败
                if (!$data['role_id'] == 0) {

                    if ($admin->create($data)) {
                        if ($admin->add()) {
                            $this->success('添加管理员成功！');
                        } else {
                            $this->error('添加管理员失败！');
                        }
                    } else {
                        $this->error($admin->getError());
                    }
                    return;
                } else {
                    $this->error('添加管理员失败！请选择管理员类型');
                }
            } else {
                $this->error('管理员账号已存在！');
            }

        }
        $this->display();
    }

    //管理员的修改页面
    public function admin_edit()
    {
        $id = I("get.id");
        if (!empty($id)) {

            $data = M('Admin')->where(array("admin_id" => $id))->select();
            $roleid = $data[0]['role_id'];
            //用role_id去查询出对应的管理员信息
            $roles = M('AdminRole')->where(array('role_id' => $roleid))->select();
            $role = M('AdminRole')->field("role_id,role_name")->select();
            foreach ($role as $val) {
                $r[$val['role_id']] = $val['role_name'];

            }
            $this->assign("data", $data[0]);
            $this->assign("r", $r);
            $this->assign("role", $role);
            $this->assign("roles", $roles);
        }
        $this->display();

    }

    //执行修改管理员信息
    public function admin_update()
    {
        $admin = M("Admin");
        if (IS_POST) {
            $data['admin_id'] = I('post.admin_id');
            $data['role_id'] = I('post.role_id');
            $data['name'] = I('post.name');
            $data['admin_name'] = I('post.admin_name');
            $data['reg_time'] = date('Y-m-d H:i:s');
            $data['login_time'] = time();
            $data['login_num'] = 0;
            $data['login_session'] = NULL;
            $data['login_ip'] = get_client_ip();
            $data['status'] = 0;
            //用admin_id查询出改用户的信息
            $admin_s = $admin->find($data['admin_id']);
            //把$admin_s['admin_pwd']赋值给$password
            $password = $admin_s['admin_pwd'];
            //判断是否有提交密码过来
            if (I('post.admin_pwd')) {
                //如果有，就把提交过来的密码赋值给$data['admin_pwd']
                $data['admin_pwd'] = sha1(I('post.admin_pwd'));
            } else {
                //如果没有，还是原来的密码赋值给$data['admin_pwd']
                $data['admin_pwd'] = $password;
            }
            if (!$data['role_id'] == 0) {
                //验证
                if ($admin->create($data)) {
                    //执行修改
                    if ($admin->save()) {

                        $this->success('修改管理员成功！');
                    } else {
                        $this->error('修改管理员失败！');
                    }

                    /*    $this->success('修改管理员成功！');*/

                } else {

                    $this->error('修改管理员失败！');
                }

            } else {
                $this->error('修改管理员失败！请设置权限！');
            }
            return;
        }

    }

    //管理员的删除
    public function admin_del()
    {
        if (IS_POST) {
            $where['admin_id'] = I("post.id");
            if (empty($where['admin_id'])) {
                $resul['info'] = "信息错误";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $role = M("Admin")->where($where)->find();
            if ($role) {
                $re = M("Admin")->where($where)->delete();
                if ($re) {
                    $re = D("LogAdmin")->insert_log("删除角色" . $role['admin_name']);
                    $resul['info'] = "删除成功";
                    $resul["status"] = 'y';
                    $this->ajaxReturn($resul, 'JSON');
                } else {
                    $resul['info'] = "系统错误";
                    $resul["status"] = 'n';
                    $this->ajaxReturn($resul, 'JSON');
                }
            } else {
                $resul['info'] = "已删除";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $this->ajaxReturn($_POST, 'JSON');
        }
        $this->display();
    }

    //角色管理页面
    public function roles()
    {
        $this->check('1_4');
        if (IS_POST) {
            $map['role_name'] = I("post.role_name");
            $_GET['p'] = 1;
        }
        if (!empty($map['role_name'])) {
            $where['role_name'] = $map['role_name'];
        }
        $count = M("AdminRole")->where($where)->count();
        $page = new \Think\Page ($count, 5, $map);
        $show = $page->show();
        $role = M('AdminRole')->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign("role", $role);
        $this->assign("page", $show);
        $this->display();

    }

    //执行角色管理添加页面
    public function roles_add()
    {
        $this->check('1_4');
        if (IS_POST) {
            $add['role_name'] = I("post.roleName");
            $add["menu_grant"] = I("post.menu_grant");
            $add['grant'] = I("post.grant");
            $add['createTime'] = time();
            if (empty($add['role_name'])) {
                $resul['info'] = "填写角色名";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            if (empty($add['grant'])) {
                $resul['info'] = "最少选择一个权限";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $re = M("AdminRole")->add($add);
            if ($re) {

                $re = D("LogAdmin")->insert_log("添加角色" . $add['role_name']);
                $resul['info'] = "添加成功";
                $resul["status"] = 'y';
                $this->ajaxReturn($resul, 'JSON');
            } else {
                $resul['info'] = "系统错误";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
        }
        $data = D("AdminMgmt")->find_ismanage();
        $this->assign("data", $data);
        $this->display();
    }

    //执行角色删除页面
    public function roles_del()
    {
        $this->check('1_4');
        if (IS_POST) {
            $where['role_id'] = I("post.id");
            if (empty($where['role_id'])) {
                $resul['info'] = "信息错误";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $role = M("AdminRole")->where($where)->find();
            if ($role) {
                $re = M("AdminRole")->where($where)->delete();
                if ($re) {
                    $re = D("LogAdmin")->insert_log("删除角色" . $role['role_name']);
                    $resul['info'] = "删除成功";
                    $resul["status"] = 'y';
                    $this->ajaxReturn($resul, 'JSON');
                } else {
                    $resul['info'] = "系统错误";
                    $resul["status"] = 'n';
                    $this->ajaxReturn($resul, 'JSON');
                }
            } else {
                $resul['info'] = "已删除";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }

            $this->ajaxReturn($_POST, 'JSON');
        }
        $this->display();
    }

    //执行角色修改页面
    public function roles_edit()
    {
        $this->check('1_4');
        if (IS_POST) {
            $where['role_id'] = I("post.id");
            if (empty($where['role_id'])) {
                $resul['info'] = "信息错误";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $save['role_name'] = I("post.roleName");
            $save["menu_grant"] = I("post.menu_grant");
            $save['grant'] = I("post.grant");
            if (empty($save['role_name'])) {
                $resul['info'] = "填写角色名";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            if (empty($save['grant'])) {
                $resul['info'] = "最少选择一个权限";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $role = M("AdminRole")->where($where)->find();
            if ($role) {
                $re = M("AdminRole")->where($where)->save($save);
                if ($re) {
                    $re = D("LogAdmin")->insert_log("编辑角色" . $role['role_name']);
                    $resul['info'] = "编辑成功";
                    $resul["status"] = 'y';
                    $this->ajaxReturn($resul, 'JSON');
                } else {
                    $resul['info'] = "系统错误";
                    $resul["status"] = 'n';
                    $this->ajaxReturn($resul, 'JSON');
                }
            } else {
                $resul['info'] = "已删除";
                $resul["status"] = 'n';
                $this->ajaxReturn($resul, 'JSON');
            }
            $this->ajaxReturn($_POST, 'JSON');
        }
        $id = I("get.id");
        if (!empty($id)) {
            $role = [];
            $menu = M("AdminRole")->where(array("role_id" => $id))->find();
            $role = D("AdminMgmt")->find_ismenu();
            $this->assign("object", $menu);
            $this->assign("data", $role);
        }
        $this->display();
    }

    //主菜单和子菜单资料显示
    public function Menu_and_submenu()
    {
        $this->check('1_0');
        $data = D("AdminMgmt")->find_ismanage();
        $arr = $arr2 = '';
        foreach ($data as $value) {
            $arr["id"] = $value["id"];
            $arr["menuname"] = $value["menuname"];
            $ob = [];
            foreach ($value["down"] as $mdc) {
                array_push($ob, $mdc["menuname"]);
            }
            $arr["down"] = implode(",", $ob);
            $arr["isallowed"] = $value["isallowed"];
            $arr["created_at"] = $value["created_at"];
            $arr2[] = $arr;
        }
        $this->assign("res", $arr2);
        $this->display();
    }

    //增加主菜单
    public function MenuStrip_add()
    {
        if (IS_POST) {
            $yao["menuname"] = I("post.menuname");
            $cha = D("AdminMgmt")->where($yao)->select();
            if (!empty($cha)) {
                $this->error("主菜单已存在,请重新输入", "MenuStrip_add", 1);
                exit();
            }
            $add['ismenu'] = "0";
            $add["isleftshow"] = "0";
            $add["ismanage"] = "1";
            $add['menuname'] = I("post.menuname");
            $add["isallowed"] = "1";
            $add["description"] = I("post.menuname");
            $add['created_at'] = time();
            $add['updated_at'] = time();
            $re = M("AdminMgmt")->add($add);
        }
        $this->display();
    }

    //编辑主菜单
    public function MenuStrip_edit()
    {
        if (IS_POST) {
            $where["id"] = I("post.mid");
            $save['ismenu'] = I("post.ismenu");
            $save["isleftshow"] = "1";
            $save["ismanage"] = I("post.ismanage");;
            $save['menuname'] = I("post.menuname");
            $save["isallowed"] = I("post.isallowed");
            $re = M("AdminMgmt")->where($where)->save($save);
            $this->success('修改成功', 'Menu_and_submenu', 1);
            exit();
        }
        $id = I("get.id");
        $data = D("AdminMgmt")->where(["id" => $id])->select();
        $this->assign('id', $id);
        $this->assign("name", $data["0"]["menuname"]);
        $this->display();
    }

    //增加子菜单
    public function submenu_add()
    {
        if (IS_POST) {
            $where["depends"] = I("post.mid");
            $shu = D("AdminMgmt")->where($where)->count();
            if ($shu != 0) {
                $add["reside"] = I("post.mid") . "_" . $shu;
            } else {
                $shu = 0;
                $add["reside"] = I("post.mid") . "_" . $shu;
            }
            $add['ismenu'] = "0";
            $add["isleftshow"] = "0";
            $add["ismanage"] = "0";
            $add["managelist"] = "1";
            $add['menuname'] = I("post.menuname");
            $add["depends"] = I("post.mid");
            $add["description"] = I("post.menuname");
            $add["url_address"] = I("post.address");
            $add["isallowed"] = "1";
            $add['created_at'] = time();
            $add['updated_at'] = time();
            $re = M("AdminMgmt")->add($add);
            $this->success('添加成功', 'Menu_and_submenu', 1);
            exit();
        }
        $id = I("get.id");
        $data = D("AdminMgmt")->where(["id" => $id])->select();
        $this->assign('id', $id);
        $this->assign("name", $data["0"]["menuname"]);
        $this->display();
    }
}
